package com.igoal.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.igoal.entity.DataStatus;
import com.igoal.entity.Tag;

@Repository("tagDAO")
public class TagDAOImpl extends GenericDaoHibernate<Tag, Long> implements
		TagDAO {

	public TagDAOImpl() {
		super(Tag.class);
	}

	public Tag findByName(String tagName) {
		DetachedCriteria dc = DetachedCriteria.forClass(Tag.class);
		dc.add(Restrictions.eq("tagName", tagName));
		getByStatus(dc, DataStatus.USING);
		
		List<Tag> result = findByCriteria(dc, 0, 1);
		if(!result.isEmpty()){
			return result.get(0);
		}
		
		return null;
	}

	public boolean exists(String tagName) {
		DetachedCriteria dc = DetachedCriteria.forClass(Tag.class);
		dc.add(Restrictions.eq("tagName", tagName));
		getByStatus(dc, DataStatus.USING);
		
		List<Tag> result = findByCriteria(dc, 0, 1);
		if(!result.isEmpty()){
			return true;
		}
		
		return false;
	}
}
